package com.mall.user.services;

import com.mall.user.IRegisterService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserRegisterRequest;
import com.mall.user.dto.UserRegisterResponse;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import com.mall.user.utils.AESUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @description:
 * @author: 李狗蛋
 * @date: 2022/1/20 21:40
 */
@Slf4j
@Component
@Service
public class RegisterServiceImpl implements IRegisterService {
    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserVerifyMapper userVerifyMapper;

    @Autowired
    JavaMailSender sendMail;

    @Override
    public UserRegisterResponse register(UserRegisterRequest request) {
        UserRegisterResponse response = new UserRegisterResponse();

        //参数校验
        request.requestCheck();

        //用户名是否重复

        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", request.getUserName());
        List<Member> members = memberMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(members)) {
            UserRegisterResponse userRegisterResponse = new UserRegisterResponse();
            userRegisterResponse.setCode(SysRetCodeConstants.USERNAME_ALREADY_EXISTS.getCode());
            userRegisterResponse.setMsg(SysRetCodeConstants.USERNAME_ALREADY_EXISTS.getMessage());
            return userRegisterResponse;
        }

        //1.向用户表插入信息
        Member member = new Member();
        member.setUsername(request.getUserName());
        member.setEmail(request.getEmail());
        //密码加密
        AESUtil aesUtil = new AESUtil(request.getUserPwd());
        String password = aesUtil.encrypt();

        member.setPassword(password);
        member.setCreated(new Date());
        member.setUpdated(new Date());
        member.setState(1);
        member.setIsVerified("N");
        int number = memberMapper.insert(member);
        //插入失败
        if (number != 1) {
            response.setCode(SysRetCodeConstants.USER_REGISTER_FAILED.getCode());
            response.setMsg(SysRetCodeConstants.USER_REGISTER_FAILED.getMessage());
            return response;
        }

        //2.向验证表插入记录
        UserVerify userVerify = new UserVerify();
        userVerify.setUsername(request.getUserName());
        //生成唯一的uuid
        String s = member.getUsername() + member.getPassword() + UUID.randomUUID().toString();
        String vUuid = DigestUtils.md5DigestAsHex(s.getBytes());
        userVerify.setUuid(vUuid);
        userVerify.setRegisterDate(new Date());
        userVerify.setIsExpire("N");
        userVerify.setIsVerify("N");
        int insert = userVerifyMapper.insert(userVerify);
        if (insert != 1) {
            response.setCode(SysRetCodeConstants.USER_REGISTER_VERIFY_FAILED.getCode());
            response.setMsg(SysRetCodeConstants.USER_REGISTER_VERIFY_FAILED.getMessage());
            return response;
        }

        //3.发送激活邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("1615975732@qq.com");
        mailMessage.setTo(request.getEmail());
        mailMessage.setSubject("注册激活");

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("http://localhost:8080/user/verify?uid=").append(vUuid).append("&").append("username=").append(request.getUserName());
        mailMessage.setText(String.valueOf(stringBuilder));

        sendMail.send(mailMessage);

        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;
    }

    @Override
    public UserVerifyResponse verifyRegister(UserVerifyRequest userVerifyRequest) {
        Example example = new Example(UserVerify.class);
        example.createCriteria().andEqualTo("uuid", userVerifyRequest.getUuid());
        UserVerify userVerify = new UserVerify();
        userVerify.setIsVerify("Y");
        int i = userVerifyMapper.updateByExampleSelective(userVerify, example);

        if (i == 1) {
            Example example1 = new Example(Member.class);
            example1.createCriteria().andEqualTo("username", userVerifyRequest.getUserName());
            Member member = new Member();
            member.setIsVerified("Y");
            int i1 = memberMapper.updateByExampleSelective(member, example1);

            UserVerifyResponse userVerifyResponse = new UserVerifyResponse();
            if (i1 == 1) {
                userVerifyResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
                return userVerifyResponse;
            }
        }

        UserVerifyResponse userVerifyResponse = new UserVerifyResponse();
        userVerifyResponse.setCode(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getCode());
        userVerifyResponse.setMsg(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getMessage());
        return userVerifyResponse;
    }


}